<template>
	<view>
		<!-- 选择按钮 -->
		<view class="address-choose-box" v-if="JSON.stringify(address)==='{}'">
			<button @click="chooseAddress" class="btnChooseAddress" type="primary" size="mini">请选择收货地址</button>
		</view>
		<!-- 收货信息盒子 -->
		<view @click="chooseAddress" class="address-info-box" v-else>
			<!-- 第一行 -->
			<view class="row1">
				<!-- 第一行左 -->
				<view class="row1-left">
					<view class="username">
						收货人：<text>{{address.userName}}</text>
					</view>
				</view>
				<!-- 第一行右 -->
				<view class="row1-right">
					<view class="phone">
						电话：<text>{{address.telNumber}}</text>
					</view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>
			<!-- 第二行 -->
			<view class="row2">
				<!-- 第二行左 -->
				<view class="row2-left">
					收货地址：
				</view>
				<!-- 第二行右 -->
				<view class="row2-right">
					{{addressStr}}
				</view>
			</view>
		</view>
		<!-- 分割线 -->
		<image class="address-border" src="/static/cart_border@2x.png"></image>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		name:"my-address",
		data() {
			return {
			};
		},
		methods:{
			...mapMutations('m_user',['updateAddress']),
	async chooseAddress(){
			const [err,succ] = await uni.chooseAddress().catch(err=>err)
			if(err===null&&succ.errMsg === 'chooseAddress:ok'){
				// this.address = succ
				this.updateAddress(succ)
			}
			// 假如用户没有授权访问收货地址,重新提起授权，前半句针对安卓机，后半句针对ios机
			if(err&&(err.errMsg==='chooseAddress:fail auth deny'||err.errMsg==='chooseAddress:fail authorize no response')){
				this.reAuth()
			}
		},
		async reAuth(){
			// 提示授权
			const [err2, confirmResult] = await uni.showModal({
				content:'检测到你狗日的没有授权读取地址，不买东西了？',
				confirmText:'授权',
				cancelText:'取消'
			})
			// 弹窗异常,直接退出
				if(err2) return
				// 点击取消
				if(confirmResult.cancel) return uni.$showMsg('不授权玩个蛋')
				// 点击确认，调用uni.openSetting()进入授权页面，让用户重新授权
				if(confirmResult.confirm) return uni.openSetting({
					// 授权结束,对授权结果再做判断
					success:(settingResult)=>{
						// 地址授权值:true,授权成功
						if(settingResult.authSetting['scope.address']){
							return uni.showMsg('授权成功，请选择地址')
							}else{
							return uni.showMsg('你取消了授权')
							}
						
					}
				})
			}
		},
		computed:{
			// state一定要在getter前面
			...mapState('m_user',['address']),
			...mapGetters('m_user',['addressStr'])
		}
	}
</script>

<style lang="scss">
	.address-choose-box{
		height: 90px;
		display: flex;
		justify-content: center;
		align-items: center;
		.btnChooseAddress{
			
		}
	}
.address-border{
	width: 100%;
	height: 5px;
}
.address-info-box{
	font-size: 12px;
	height: 90px;
	display: flex;
	flex-direction: column;
	justify-content: center;
	padding: 0 5px;
	.row1{
		display: flex;
		justify-content: space-between;
		.row1-right{
			display: flex;
			justify-content: space-between;
		}
	}
	.row2{
		display: flex;
		align-items: center;
		margin-top: 10px;
		.row2-left{
			white-space: nowrap;
		}
}
}
</style>
